package chpt12.dao;

import java.util.List;
import java.util.Iterator;

class StudentModel {
	private StudentDao sdao; // DAO对象
	private String[] tbHead;
	private String[][] tbData;
	private int columnNum = 5; // 记录的字段数（固定）

	public StudentModel() {
		sdao = new StudentDaoImpl(); // 初始化DAO对象
	}

	String[] getTableHead() { // 获取表格的标题信息
		tbHead = new String[columnNum];
		tbHead[0] = "id";
		tbHead[1] = "学号";
		tbHead[2] = "姓名";
		tbHead[3] = "GPA";
		tbHead[4] = "生日";
		return tbHead;
	}

	String[][] getTableData() { // 获取表格的数据信息
		return tbData;
	}

	void queryStudentAll() { // 查询所有学生
		List<Student> list = sdao.getAllStudents();
		fillStudentData(list);
	}

	void queryStudentByName(String name) { // 按姓名查询学生
		List<Student> list = sdao.getStudentsByName(name);
		fillStudentData(list);
	}

	public void deleteStudentById(Long id) {
		sdao.deleteStudentById(id);
	}

	private void fillStudentData(List<Student> list) { // 用查询结果填充表格数据
		int recNumber = list.size();// 获取记录数
		tbData = new String[recNumber][columnNum];
		Iterator<Student> it = list.iterator();
		int recNO = 0;
		while (it.hasNext()) {
			Student stu = it.next();
			tbData[recNO][0] = stu.getId() + ""; // id
			tbData[recNO][1] = stu.getSno(); // 学号
			tbData[recNO][2] = stu.getName(); // 姓名
			tbData[recNO][3] = stu.getGpa() + ""; // GPA
			tbData[recNO][4] = stu.getBday() + ""; // 学号
			recNO++;
		}
	}
}
